$('.spec-add').click(function () {
    //构造spec
    var html = '';
    //查找specdata
    var obj = $('.specData').last();
    var nums = $(obj).attr('nums');
    if(nums == undefined){
        nums = 1;
    }else{
        nums++;
    }
    //搭建一个框架
    html = `
    <div class="specData spec${nums}" nums="${nums}">
        <span class="spec-edit-${nums}">
            <input type="number" value="0" class="form-control spec-sort-${nums}" placeholder="排序" style="width: 100px;">
            <input type="text" class="form-control spec-value-${nums}" placeholder="规格名" style="width: 100px;">
            <a href="javascript:;" class="btn btn-sm btn-success" nums="${nums}" onclick="specAdd(this);">确认</a>
            <a href="javascript:;" class="btn btn-sm btn-danger" nums="${nums}" onclick="specDel(this,0)">取消</a>
        </span>
        <div class="sepcValue spec-value-${nums}">
            <div style="margin-left: 10px;" class="clearfix spec-value-list-${nums}">
            </div>
        </div>
    </div>`;
    $('.spec-list').append(html);
})

/**
 * 规格名添加
 * @param obj
 */
function specAdd(obj,is_checked=false,id='') {
    var nums = $(obj).attr('nums');
    var values = $('.spec-value-'+nums).val();
    var sort = $('.spec-sort-'+nums).val();
    if(values == '' || values == undefined || values == null){
        alert('请填写规格名');return false;
    }
    $('.spec-edit-'+nums).remove();
    if(is_checked){
        $('.spec-th-'+nums).text(values);
        var check_str = 'checked="checked"';

    }else{
        var check_str = '';
    }
    var html = '';
    html = `
    <input type="checkbox" ${check_str} onclick="selectSpec(this)" nums="${nums}" class="selectspecdata selectspecdata-${nums}" name="specdata[${nums}][spec][is_checked]" value="1">
        <span>${sort}：${values}
            <input type="hidden" name="specdata[${nums}][spec][sort]" value="${sort}">
            <input type="hidden" name="specdata[${nums}][spec][name]" value="${values}">
            <input type="hidden" name="specdata[${nums}][spec][id]" value="${id}">
            <a href="javascript:;" title="规格值" onclick="specValueAdd(this)" nums="${nums}"><i class="fa fa-plus"></i></a>
            <a href="javascript:;" title="修改" onclick="specEdit(this)" nums="${nums}"><i class="fa fa-pencil"></i></a>
            <a href="javascript:;" title="删除" onclick="specDel(this,0)" nums="${nums}"><i class="fa fa-trash"></i></a>
        </span>
    `;
    $('.spec-value-'+nums).before(html);
}

/**
 * 规格更改
 * @param obj
 */
function specEdit(obj) {
    var nums = $(obj).attr('nums');
    var values = $("input[name='specdata[" + nums + "][spec][name]']").val();
    var sort = $("input[name='specdata[" + nums + "][spec][sort]']").val();
    var is_checked = $('.selectspecdata-'+nums).is(':checked');
    var id = $("input[name='specdata[" + nums + "][spec][id]']").val();
    var html = '';
    html = `
       <span class="spec-edit-${nums}">
            <input type="number" value="${sort}" class="form-control spec-sort-${nums}" placeholder="排序" style="width: 100px;">
            <input type="text" value="${values}" class="form-control spec-value-${nums}" placeholder="规格名" style="width: 100px;">
            <a href="javascript:;" class="btn btn-sm btn-success" nums="${nums}" onclick="specAdd(this,${is_checked},${id});">确认</a>
            <a href="javascript:;" class="btn btn-sm btn-danger" nums="${nums}" onclick="specDel(this,1,${is_checked},${id})">取消</a>
        </span>
    
    `;
    $('.spec-value-' + nums).siblings().remove();
    $('.spec-value-' + nums).before(html);
}

/**
 * 规格名删除
 * @param obj
 */
function specDel(obj,type,is_checked=false,id='') {
    var nums = $(obj).attr('nums');

    //直接移除
    if(type == 0){
        var is_checked = $('.selectspecdata-'+nums).is(':checked');
        //移除前进行下方表单更新
        $('.spec'+nums).remove();
        if(is_checked){
            createTable();
        }
    }else if(type == 1){
        //恢复原样
        var values = $('.spec-value-'+nums).val();
        var sort = $('.spec-sort-'+nums).val();
        if(values == '' || values == undefined || values == null){
            alert('请填写规格名');return false;
        }
        if(is_checked){
            var check_str = 'checked="checked"';
        }else{
            var check_str = '';
        }
        $('.spec-edit-'+nums).remove();
        var html = '';
        html = `
            <input type="checkbox" ${check_str} onclick="selectSpec(this)" nums="${nums}" class="selectspecdata selectspecdata-${nums}" name="specdata[${nums}][spec][is_checked]" value="1">
            <span>${sort}：${values}
                <input type="hidden" name="specdata[${nums}][spec][sort]" value="${sort}">
                <input type="hidden" name="specdata[${nums}][spec][name]" value="${values}">
                <input type="hidden" name="specdata[${nums}][spec][id]" value="${id}">
                <a href="javascript:;" title="规格值" onclick="specValueAdd(this)" nums="${nums}"><i class="fa fa-plus"></i></a>
                <a href="javascript:;" title="修改" onclick="specEdit(this)" nums="${nums}"><i class="fa fa-pencil"></i></a>
                <a href="javascript:;" title="删除" onclick="specDel(this,0)" nums="${nums}"><i class="fa fa-trash"></i></a>
            </span>
        `;
        $('.spec-value-'+nums).before(html);

    }


}

/**
 * 规格值添加
 */
function specValueAdd(obj) {
    var nums = $(obj).attr('nums');
    var son_obj = $('.spec-value-info-'+nums).last();
    var son_nums = $(son_obj).attr('son');
    if(son_nums == undefined){
        son_nums = 1;
    }else{
        son_nums++;
    }
    var html = '';
    html = `
    <div style="margin: 10px;float: left" class="spec-value-info-${nums} spec-value-info-${nums}-${son_nums}" parent="${nums}" son="${son_nums}">
        <span>
            <input type="number" value="0" class="form-control spec-value-sort-${nums}-${son_nums}" placeholder="排序" style="width: 100px;">
            <input type="text" class="form-control spec-value-name-${nums}-${son_nums}" placeholder="规格值" style="width: 100px;">
            <a href="javascript:;" class="btn btn-sm btn-success" onclick="specValueAdded(this)" nums="${nums}" son="${son_nums}">确认</a>
            <a href="javascript:;" class="btn btn-sm btn-danger" onclick="specValueDel(this,0)" nums="${nums}" son="${son_nums}">取消</a>
        </span>
    </div>
    `;
    $('.spec-value-list-'+nums).append(html);
}

/**
 * 规格值执行添加
 */
function specValueAdded(obj,is_checked=false,id='') {
    var nums = $(obj).attr('nums');
    var son_nums = $(obj).attr('son');
    var values = $('.spec-value-name-'+nums+'-'+son_nums).val();
    var sort = $('.spec-value-sort-'+nums+'-'+son_nums).val();
    if(values == '' || values == undefined || values == null){
        alert('请填写规格值');return false;
    }
    if(is_checked){
        $('.spec-value-td-'+nums+'-'+son_nums).text(values);
        var check_str = 'checked="checked"';

    }else{
        var check_str = '';
    }
    var html = '';
    html = `
    <input type="checkbox"  ${check_str} onclick="selectSpecValue(this)" nums="${nums}" son="${son_nums}" class="selectspecinfodata-${nums} selectspecinfodata-${nums}-${son_nums}" name="specdata[${nums}][spec_value][${son_nums}][is_checked]" value="1">
    <!--                    不做更改状态-->
    <span>${sort}：${values}
        <input type="hidden" name="specdata[${nums}][spec_value][${son_nums}][sort]" value="${sort}">
        <input type="hidden" name="specdata[${nums}][spec_value][${son_nums}][name]" value="${values}">
        <input type="hidden" name="specdata[${nums}][spec_value][${son_nums}][id]" value="${id}">
        <a href="javascript:;" onclick="specValueEdit(this)" nums="${nums}" son="${son_nums}"><i class="fa fa-pencil"></i></a>
        <a href="javascript:;" onclick="specValueDel(this,0)" nums="${nums}" son="${son_nums}"><i class="fa fa-trash"></i></a>
    </span>
    `;
    $('.spec-value-info-'+nums+'-'+son_nums).empty();
    $('.spec-value-info-'+nums+'-'+son_nums).append(html);

}

/**
 * 规格值编辑
 * @param obj
 */
function specValueEdit(obj) {
    var nums = $(obj).attr('nums');
    var son_nums = $(obj).attr('son');
    var values = $("input[name='specdata[" + nums + "][spec_value]["+son_nums+"][name]']").val();
    var sort = $("input[name='specdata[" + nums + "][spec_value]["+son_nums+"][sort]']").val();
    var id = $("input[name='specdata[" + nums + "][spec_value]["+son_nums+"][id]']").val();
    if(values == '' || values == undefined || values == null){
        alert('请填写规格值');return false;
    }
    //检测是否可编辑
    var fatherLength = $('.selectspecdata-'+nums).length;
    if(fatherLength != 1){
        alert('请先编辑完规格名后修改规格值');return;
    }
    //获取规格值是否被选中
    var is_checked = $('.selectspecinfodata-'+nums+'-'+son_nums).is(':checked');
    var html = '';
    html = `
    <div style="margin: 10px;float: left" class="spec-value-info-${nums} spec-value-info-${nums}-${son_nums}" parent="${nums}" son="${son_nums}">
        <span>
            <input type="number" value="${sort}" class="form-control spec-value-sort-${nums}-${son_nums}" placeholder="排序" style="width: 100px;">
            <input type="text" value="${values}" class="form-control spec-value-name-${nums}-${son_nums}" placeholder="规格值" style="width: 100px;">
            <a href="javascript:;" class="btn btn-sm btn-success" onclick="specValueAdded(this,${is_checked},${id})" nums="${nums}" son="${son_nums}">确认</a>
            <a href="javascript:;" class="btn btn-sm btn-danger" onclick="specValueDel(this,1,${is_checked},${id})" nums="${nums}" son="${son_nums}">取消</a>
        </span>
    </div>
    `;
    $('.spec-value-info-'+nums+'-'+son_nums).empty();
    $('.spec-value-info-'+nums+'-'+son_nums).append(html);

}

/**
 * 规格值删除
 * @param obj
 */
function specValueDel(obj,type,is_checked=false,id='') {
    var nums = $(obj).attr('nums');
    var son_nums = $(obj).attr('son');
    if(type == 0){
        var is_checked = $('.selectspecinfodata-'+nums+'-'+son_nums).is(':checked');
        //直接移除
        $('.spec-value-info-'+nums+'-'+son_nums).remove();
        // if(is_checked){
        //     createTable();
        // }
        var cancelFa = true;
        //查看父类下还有没其他选中的选项
        var fatherobj = $('.selectspecinfodata-'+nums);
        for (var i = 0; i<fatherobj.length; i++){
            var is_checkeds = fatherobj.eq(i).is(':checked');
            if(is_checkeds){
                cancelFa = false;
            }
        }
        if(cancelFa){
            $('.selectspecdata-'+nums).prop('checked','');
        }
        //发生变化时才变更表格
        if(is_checked){
            createTable();
        }
    }else if (type == 1){
        //恢复原样
        var values = $('.spec-value-name-'+nums+'-'+son_nums).val();
        var sort = $('.spec-value-sort-'+nums+'-'+son_nums).val();
        if(values == '' || values == undefined || values == null){
            alert('请填写规格值');return false;
        }
        if(is_checked){
            var check_str = 'checked="checked"';

        }else{
            var check_str = '';
        }
        var html = '';
        html = `
                <input type="checkbox" ${check_str} onclick="selectSpecValue(this)" nums="${nums}" son="${son_nums}" class="selectspecinfodata-${nums} selectspecinfodata-${nums}-${son_nums}" value="1" name="specdata[${nums}][spec_value][${son_nums}][is_checked]">
                <!--                    不做更改状态-->
                <span>${sort}：${values}
                    <input type="hidden" name="specdata[${nums}][spec_value][${son_nums}][sort]" value="${sort}">
                    <input type="hidden" name="specdata[${nums}][spec_value][${son_nums}][name]" value="${values}">
                    <input type="hidden" name="specdata[${nums}][spec_value][${son_nums}][id]" value="${id}">
                    <a href="javascript:;" onclick="specValueEdit(this)" nums="${nums}" son="${son_nums}"><i class="fa fa-pencil"></i></a>
                    <a href="javascript:;" onclick="specValueDel(this,0)" nums="${nums}" son="${son_nums}"><i class="fa fa-trash"></i></a>
                </span>
            `;
        $('.spec-value-info-'+nums+'-'+son_nums).empty();
        $('.spec-value-info-'+nums+'-'+son_nums).append(html);
    }
}

/**
 * 选择规格名
 * @param obj
 */
function selectSpec(obj) {
    var nums = $(obj).attr('nums');
    var is_checked = $(obj).is(':checked');
    //进行重组
    if(is_checked){
        var lengths = $('.selectspecinfodata-'+nums).length;
        if(lengths<1){
            alert('请添加规格值后选择');
            $(obj).prop('checked','');
            return;
        }
        //将规格值全部选中
        $('.selectspecinfodata-'+nums).prop('checked','checked');
    }else{
        $('.selectspecinfodata-'+nums).prop('checked','');
    }
    createTable();
}

/**
 * 选择规格值
 * @param obj
 */
function selectSpecValue(obj) {
    var nums = $(obj).attr('nums');
    var son_nums = $(obj).attr('nums');
    var is_checked = $(obj).is(':checked');
    var fatherLength = $('.selectspecdata-'+nums).length;
    if(fatherLength != 1){
        alert('请先编辑规格名后选择规格值');return;
    }
    //进行重组
    if(is_checked){
        //查看父级是否选中
        var is_father_checked = $('.selectspecdata-'+nums).is(':checked');
        if(!is_father_checked){
            //选中父类
            $('.selectspecdata-'+nums).prop('checked','checked');
        }

    }else{
        var cancelFa = true;
        //查看父类下还有没其他选中的选项
        var fatherobj = $('.selectspecinfodata-'+nums);
        for (var i = 0; i<fatherobj.length; i++){
            var is_checkeds = fatherobj.eq(i).is(':checked');
            if(is_checkeds){
                cancelFa = false;
            }
        }
        if(cancelFa){
            $('.selectspecdata-'+nums).prop('checked','');
        }
    }
    createTable();
}

/**
 * 格式化表格
 */
function createTable() {
    //获取所有选中的
    var allarr = [];
    var allspec = $('.selectspecdata');
    var allnums = allspec.length;
    var arrtitle = [];
    for (var i = 0;i<allnums;i++){
        var other_is_checked = allspec.eq(i-1).is(':checked');
        if(other_is_checked){
            var other_nums = allspec.eq(i-1).attr('nums');
            arrtitle.push(other_nums);
            //获取下面所有的被选中的
            var arr = [];
            var allspecson = $('.selectspecinfodata-'+other_nums);
            var allnumsson = allspecson.length;
            for (var j=0;j<allnumsson;j++){
                var other_son_is_checked = allspecson.eq(j-1).is(':checked');
                if(other_son_is_checked){
                    var other_son_nums = allspecson.eq(j-1).attr('son');
                    var str = other_nums +'-'+ other_son_nums;
                    arr.push(str);
                }
            }
            allarr.push(arr);
        }
    }
    //将获取到的数组进行重组
    var list = calcDescartes(allarr);
    var arrlength = list.length;
    var html = "<table class='sku-table table table-bordered'><tr>";
    for (var i = 0; i<arrtitle.length; i++){
        var nums = arrtitle[i];
        var title = $("input[name='specdata["+nums+"][spec][name]']").val();
        var classed = 'spec-th-'+nums;
        html += "<th class='"+classed+"' center-table>"+title+"</th>";
    }
    html += '<th class="center-table">爆款价</th><th class="center-table">重量</th><th class="center-table">库存</th><th class="center-table">缩略图</th></tr>';
    for (var j = 0; j<arrlength;j++){
        var is_arrays = $.isArray(list[j]);
        if(is_arrays){
            //数组生成多行多列
            html += "<tr class='spec-tr'>";
            for (var h = 0; h<list[j].length;h++){
                var codes = list[j][h];
                var codes_arr = codes.split('-');
                var nums = codes_arr[0];
                var son_nums = codes_arr[1];
                var son_title = $("input[name='specdata["+nums+"][spec_value]["+son_nums+"][name]']").val();
                var son_classed = 'spec-value-td-'+nums+'-'+son_nums;
                html += "<td class='"+ son_classed +" center-table'>"+son_title+"</td>";
            }
            var sku_data = list[j].join(',');
            //将之前填写的数据拿过来用
            var price = '',weight = '',sku_stock = '',sku_thumbnail = '',image_str = '',id = '';
            var price1 = $("input[name='sku["+j+"][price]']").val();
            if(price1 != undefined && price1 != ''){
                price = price1;
            }
            var weight1 = $("input[name='sku["+j+"][weight]']").val();
            if(weight1 != undefined && weight1 != ''){
                weight = weight1;
            }
            var sku_stock1 = $("input[name='sku["+j+"][sku_stock]']").val();
            if(sku_stock1 != undefined && sku_stock1 != ''){
                sku_stock = sku_stock1;
            }
            var sku_thumbnail1 = $("input[name='sku["+j+"][sku_thumbnail]']").val();
            if(sku_thumbnail1 != undefined && sku_thumbnail1 != ''){
                sku_thumbnail = sku_thumbnail1;
                image_str = ' style="background-image:url('+sku_thumbnail+')" ';
            }
            var id1 = $("input[name='sku["+j+"][id]']").val();
            if(id1 != undefined && id1 != ''){
                id = id1;
            }
            html += "<td class='center-table'><input type='hidden' name='sku["+j+"][id]' value='"+id+"'><input type='hidden' name='sku["+j+"][data]' value='"+sku_data+"'><input type='number' data-rule='required' class='form-control price-values' step='0.01' name='sku["+j+"][price]' value='"+price+"'></td>";
            html += "<td class='center-table'><input type='number' data-rule='required' class='form-control weight-values' step='0.01' name='sku["+j+"][weight]' value='"+weight+"'></td>";
            html += "<td class='center-table'><input type='number' data-rule='required' class='form-control stock-values' name='sku["+j+"][sku_stock]' value='"+sku_stock+"'></td>";
            html += "<td class='center-table'><label class='spec-label sku-thumbnail-label-"+j+"' "+image_str+" for='sku-thumbnail-"+j+"'><input nums='"+j+"' accept='image/*' type='file' onchange='uploadImg(this)' style='display: none;' id='sku-thumbnail-"+j+"'></label><input class='sku-thumbnail-"+j+"' type='hidden' name='sku["+j+"][sku_thumbnail]' value='"+sku_thumbnail+"'></td>";
            html += "</tr>";
        }else{
            html += "<tr class='spec-tr'>";
            //非数组仅生成一列多行
            var codes = list[j];
            var codes_arr = codes.split('-');
            var nums = codes_arr[0];
            var son_nums = codes_arr[1];
            var son_title = $("input[name='specdata["+nums+"][spec_value]["+son_nums+"][name]']").val();
            var son_classed = 'spec-value-td-'+nums+'-'+son_nums;
            html += "<td class='"+ son_classed +" center-table'>"+son_title+"</td>";
            var sku_data = codes;
            var price = '',weight = '',sku_stock = '',sku_thumbnail = '',image_str = '',id = '';
            var price1 = $("input[name='sku["+j+"][price]']").val();
            if(price1 != undefined && price1 != ''){
                price = price1;
            }
            var weight1 = $("input[name='sku["+j+"][weight]']").val();
            if(weight1 != undefined && weight1 != ''){
                weight = weight1;
            }
            var sku_stock1 = $("input[name='sku["+j+"][sku_stock]']").val();
            if(sku_stock1 != undefined && sku_stock1 != ''){
                sku_stock = sku_stock1;
            }
            var sku_thumbnail1 = $("input[name='sku["+j+"][sku_thumbnail]']").val();
            if(sku_thumbnail1 != undefined && sku_thumbnail1 != ''){
                sku_thumbnail = sku_thumbnail1;
                image_str = ' style="background-image:url('+sku_thumbnail+')" ';
            }
            var id1 = $("input[name='sku["+j+"][id]']").val();
            if(id1 != undefined && id1 != ''){
                id = id1;
            }
            html += "<td class='center-table'><input type='hidden' name='sku["+j+"][id]' value='"+id+"'><input type='hidden' name='sku["+j+"][data]' value='"+sku_data+"'><input type='number' class='form-control price-values' step='0.01' data-rule='required' name='sku["+j+"][price]' value='"+price+"'></td>";
            html += "<td class='center-table'><input type='number' class='form-control weight-values' step='0.01' data-rule='required' name='sku["+j+"][weight]' value='"+weight+"'></td>";
            html += "<td class='center-table'><input type='number' class='form-control stock-values' name='sku["+j+"][sku_stock]' data-rule='required' value='"+sku_stock+"'></td>";
            html += "<td class='center-table'><label class='spec-label sku-thumbnail-label-"+j+"' "+image_str+" for='sku-thumbnail-"+j+"'><input nums='"+j+"' accept='image/*' type='file' onchange='uploadImg(this)' style='display: none;' id='sku-thumbnail-"+j+"'></label><input class='sku-thumbnail-"+j+"' type='hidden' name='sku["+j+"][sku_thumbnail]' value='"+sku_thumbnail+"'></td>";
            html += "</tr>";
        }
    }
    html += '</table>';
    $('.specTable').empty();
    $('.specTable').append(html);
    for (var n = 0; n<arrtitle.length; n++){
        mergeTable(n);
    }

}

/**
 * 合并单元格（第几列有重复的进行合并）
 * @param colindex
 */
function mergeTable(colIdx) {
    // var tableObj = $('');
    $(".sku-table").each(function(){
        var that;
        $('tr', this).each(function(row) {
            $('td:eq('+colIdx+')', this).filter(':visible').each(function(col) {
                if (that!=null && $(this).html() == $(that).html()) {
                    rowspan = $(that).attr("rowSpan");
                    if (rowspan == undefined) {
                        $(that).attr("rowSpan",1);
                        rowspan = $(that).attr("rowSpan");
                    }
                    rowspan = Number(rowspan)+1;
                    $(that).attr("rowSpan",rowspan);
                    $(this).hide();
                } else {
                    that = this;
                }
            });
        });
    });
}

/**
 * 笛卡尔积合并
 * @param array
 * @returns {*|*[]|U}
 */
function calcDescartes (array) {
    if (array.length < 2) return array[0] || [];
    return [].reduce.call(array, function (col, set) {
        var res = [];
        col.forEach(function (c) {
            set.forEach(function (s) {
                var t = [].concat(Array.isArray(c) ? c : [c]);
                t.push(s);
                res.push(t);
            })
        });
        return res;
    });
}

/**
 * 单独上传图片
 */
function uploadImg(obj) {
    var nums = $(obj).attr('nums');
    $.ajax({})
    //预览class = sku-thumbnail-label-"+nums+"-"+son_nums+"
    //存储class = sku-thumbnail-"+nums+"-"+son_nums+"
    var file = $(obj).get(0).files[0];
    require(['upload'], function(Upload){
        Upload.api.send(file, function(data, ret){
            $('.sku-thumbnail-label-'+nums).css('backgroundImage','url('+data.url+')');
            $(".sku-thumbnail-"+nums).val(data.url);
        }, function(data, ret){
            Toastr.success("失败");
        });
    });

}

/**
 * 编辑时调用改方法
 */
function createTableEdit(lists){
    //获取所有选中的
    var allarr = [];
    var allspec = $('.selectspecdata');
    var allnums = allspec.length;
    var arrtitle = [];
    for (var i = 0;i<allnums;i++){
        var other_is_checked = allspec.eq(i-1).is(':checked');
        if(other_is_checked){
            var other_nums = allspec.eq(i-1).attr('nums');
            arrtitle.push(other_nums);
            //获取下面所有的被选中的
            var arr = [];
            var allspecson = $('.selectspecinfodata-'+other_nums);
            var allnumsson = allspecson.length;
            for (var j=0;j<allnumsson;j++){
                var other_son_is_checked = allspecson.eq(j-1).is(':checked');
                if(other_son_is_checked){
                    var other_son_nums = allspecson.eq(j-1).attr('son');
                    var str = other_nums +'-'+ other_son_nums;
                    arr.push(str);
                }
            }
            allarr.push(arr);
        }
    }
    //将获取到的数组进行重组
    var list = calcDescartes(allarr);
    var arrlength = list.length;
    var html = "<table class='sku-table table table-bordered'><tr>";
    for (var i = 0; i<arrtitle.length; i++){
        var nums = arrtitle[i];
        var title = $("input[name='specdata["+nums+"][spec][name]']").val();
        var classed = 'spec-th-'+nums;
        html += "<th class='"+classed+"' center-table>"+title+"</th>";
    }
    html += '<th class="center-table">爆款价</th><th class="center-table">重量</th><th class="center-table">库存</th><th class="center-table">缩略图</th></tr>';
    for (var j = 0; j<arrlength;j++){
        var is_arrays = $.isArray(list[j]);
        if(is_arrays){
            //数组生成多行多列
            html += "<tr class='spec-tr'>";
            for (var h = 0; h<list[j].length;h++){
                var codes = list[j][h];
                var codes_arr = codes.split('-');
                var nums = codes_arr[0];
                var son_nums = codes_arr[1];
                var son_title = $("input[name='specdata["+nums+"][spec_value]["+son_nums+"][name]']").val();
                var son_classed = 'spec-value-td-'+nums+'-'+son_nums;
                html += "<td class='"+ son_classed +" center-table'>"+son_title+"</td>";
            }
            var ks = list[j];
            var sku_data = list[j].join(',');
            var ks1 = ks.sort().join(',');
            var id = lists[ks1].id;
            var price = lists[ks1].price;
            var weight = lists[ks1].weight;
            var sku_thumbnail = lists[ks1].sku_thumbnail;
            var sku_stock = lists[ks1].sku_stock;
            var img_style = '';
            if(sku_thumbnail != ''){
                img_style = "style='background-image:url("+sku_thumbnail+");'";
            }
            html += "<td class='center-table'><input type='hidden' name='sku["+j+"][id]' value='"+id+"'><input type='hidden' name='sku["+j+"][data]' value='"+sku_data+"'><input type='number' data-rule='required' class='form-control price-values' step='0.01' name='sku["+j+"][price]' value='"+price+"'></td>";
            html += "<td class='center-table'><input type='number' data-rule='required' class='form-control weight-values' step='0.01' name='sku["+j+"][weight]' value='"+weight+"'></td>";
            html += "<td class='center-table'><input type='number' data-rule='required' class='form-control stock-values' name='sku["+j+"][sku_stock]' value='"+sku_stock+"'></td>";
            html += "<td class='center-table'><label class='spec-label sku-thumbnail-label-"+j+"' "+img_style+"  for='sku-thumbnail-"+j+"'><input nums='"+j+"' accept='image/*' type='file' onchange='uploadImg(this)' style='display: none;' id='sku-thumbnail-"+j+"'></label><input class='sku-thumbnail-"+j+"' type='hidden' name='sku["+j+"][sku_thumbnail]' value='"+sku_thumbnail+"'></td>";
            html += "</tr>";
        }else{
            html += "<tr class='spec-tr'>";
            //非数组仅生成一列多行
            var codes = list[j];
            var codes_arr = codes.split('-');
            var nums = codes_arr[0];
            var son_nums = codes_arr[1];
            var son_title = $("input[name='specdata["+nums+"][spec_value]["+son_nums+"][name]']").val();
            var son_classed = 'spec-value-td-'+nums+'-'+son_nums;
            html += "<td class='"+ son_classed +" center-table'>"+son_title+"</td>";
            var sku_data = codes;
            var ks1 = sku_data;
            var id = lists[ks1].id;
            var price = lists[ks1].price;
            var weight = lists[ks1].weight;
            var sku_thumbnail = lists[ks1].sku_thumbnail;
            var sku_stock = lists[ks1].sku_stock;
            var img_style = '';
            if(sku_thumbnail != ''){
                img_style = "style='background-image:url("+sku_thumbnail+");'";
            }
            html += "<td class='center-table'><input type='hidden' name='sku["+j+"][id]' value='"+id+"'><input type='hidden' name='sku["+j+"][data]' value='"+sku_data+"'><input type='number' data-rule='required' class='form-control price-values' step='0.01' name='sku["+j+"][price]' value='"+price+"'></td>";
            html += "<td class='center-table'><input type='number' data-rule='required' class='form-control weight-values' step='0.01' name='sku["+j+"][weight]' value='"+weight+"'></td>";
            html += "<td class='center-table'><input type='number' data-rule='required' class='form-control stock-values' name='sku["+j+"][sku_stock]' value='"+sku_stock+"'></td>";
            html += "<td class='center-table'><label class='spec-label sku-thumbnail-label-"+j+"' "+img_style+"  for='sku-thumbnail-"+j+"'><input nums='"+j+"' accept='image/*' type='file' onchange='uploadImg(this)' style='display: none;' id='sku-thumbnail-"+j+"'></label><input class='sku-thumbnail-"+j+"' type='hidden' name='sku["+j+"][sku_thumbnail]' value='"+sku_thumbnail+"'></td>";
            html += "</tr>";
        }
    }
    html += '</table>';
    $('.specTable').empty();
    $('.specTable').append(html);
    for (var n = 0; n<arrtitle.length; n++){
        mergeTable(n);
    }
}

/**
 * 批量设置
 * @param type 0原价 1价格 2库存 3重量
 */
function batchSet(type,model) {
    switch (type) {
        case 0:
            var origin_price = $('#batch-orgin-price').val();
            //更改所有为空的原价
            $('.original_price-values').each(function () {
                var old_origin_price = $(this).val();
                if(model == 1){
                    $(this).val(origin_price);
                }else{
                    if(old_origin_price == '' || old_origin_price == undefined || old_origin_price == null){
                        $(this).val(origin_price);
                    }
                }
            })
            break;
        case 1:
            //更改所有为空的价格
            var price = $('#batch-price').val();
            $('.price-values').each(function () {
                var old_price = $(this).val();
                if(model == 1){
                    $(this).val(price);
                }else{
                    if(old_price == '' || old_price == undefined || old_price == null){
                        $(this).val(price);
                    }
                }
            })
            break;
        case 2:
            //更改所有库存
            var stock = $('#batch-stock').val();
            $('.stock-values').each(function () {
                var old_stock = $(this).val();
                if(model == 1){
                    $(this).val(stock);
                }else{
                    if(old_stock == '' || old_stock == undefined || old_stock == null){
                        $(this).val(stock);
                    }
                }
            })
            break;
        case 3:
            var weight = $('#batch-weight').val();
            $('.weight-values').each(function () {
                var old_weight = $(this).val();
                if(model == 1){
                    $(this).val(weight);
                }else{
                    if(old_weight == '' || old_weight == undefined || old_weight == null){
                        $(this).val(weight);
                    }
                }
            })
            //更改所有重量
            break;
    }
}

